<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Controller\CommonController;

/**
 * 功能锁
 */
class BlockController extends CommonController {

    public  function index() {
        $v_model = M('FrontFunBlock');

        $total = $v_model->where($map)->count();
        $page = new \Think\Page($total, 100);
        $show = $page->show();

        $vList = $v_model->where($map)
            ->limit($page->firstRow .',' . $page->listRows)
            ->order('id DESC')
            ->select();


        $ios_vers_list = M('FrontVersion')->where(array('type' => 'ios'))->order('id desc')->select();
        $android_vers_list = M('FrontVersion')->where(array('type' => 'android'))->order('id desc')->select();

        foreach($vList as $key => $val) {
            $vList[$key]['ios_vers'] = M('FrontVersion')->getFieldById($val['ios_min_vers_id'],'version');
            $vList[$key]['android_vers'] = M('FrontVersion')->getFieldById($val['android_min_vers_id'],'version');
        }

        $this->assign('showpage', $show);
        $this->assign('module', array('article'=>'文稿','live'=>'直播'));
        $this->assign('action', array('add'=>'发布','edit'=>'修改','view'=>'查看','delete'=>'删除'));
        $this->assign('vList', $vList);
        $this->assign('ios_vers_list', $ios_vers_list);
        $this->assign('android_vers_list', $android_vers_list);
        $this->display();
    }


    public function add() {
        if(IS_POST) {
            $model = D('FrontFunBlock');

            if($model->create()) {
                $ret = $model->add();
                if($ret) {
                    $this->success('添加成功');
                } else {
                    $this->error('添加失败');
                }

            } else {
                $this->error($model->getError());
            }
        }
    }


    public function edit() {
        if(IS_POST) {
            $model = D('FrontFunBlock');

            if($model->create()) {
                $ret = $model->save();
                if($ret) {
                    $this->success('修改成功');
                } else {
                    $this->error('修改失败');
                }

            } else {
                $this->error($model->getError());
            }
        }
    }

    public function ajaxGetBlockInfo($id) {
        $model =M('FrontFunBlock');
        if($id){
            $info=$model->where('id='.$id)->find();
            if($info){
                $this->ajaxReturn($info);
            }else{
                $this->ajaxReturn(0);
            }
        }else{
            return false;
        }
    }

    public function ajaxdelblock($id) {
        $model=M('FrontFunBlock');
        if($id){
            $map['id'] = $id;
            $ret = $model->where($map)->delete();
            if($ret){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn(0);
            }
        }else{
            return false;
        }
    }
}